package io.awen.modules.sign.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import io.awen.common.exception.RRException;
import io.awen.common.utils.PageUtils;
import io.awen.common.utils.Query;
import io.awen.modules.sign.dao.SignVillageTownDao;
import io.awen.modules.sign.entity.SignCarLoadRecordEntity;
import io.awen.modules.sign.entity.SignContainerEntity;
import io.awen.modules.sign.entity.SignVillageTownEntity;
import io.awen.modules.sign.qo.SignVillageTownQo;
import io.awen.modules.sign.service.SignContainerService;
import io.awen.modules.sign.service.SignFleetService;
import io.awen.modules.sign.service.SignVillageTownService;
import io.awen.modules.sign.service.SignUnitService;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.PostConstruct;
import java.util.*;
import java.util.concurrent.ScheduledExecutorService;


@Service("signVillageTownService")
public class SignVillageTownServiceImpl extends ServiceImpl<SignVillageTownDao, SignVillageTownEntity> implements SignVillageTownService {


//    @Override
//    public SignVillageTownEntity findVillageTown(Long id) {
//        Iterator<SignVillageTownEntity> iterator = sensorVillageTownMap.values().iterator();
//        while (iterator.hasNext()) {
//            SignVillageTownEntity VillageTown = iterator.next();
//            if (VillageTown.getId().equals(id)) {
//                return VillageTown;
//            }
//        }
//        return null;
//    }

    @Autowired
    private SignContainerService signContainerService;

    @Override
    public PageUtils queryPage(SignVillageTownQo qo) {
        IPage<SignVillageTownEntity> page = this.page(new Query<SignVillageTownEntity>().getPage(qo), new QueryWrapper<SignVillageTownEntity>().lambda().orderByDesc(SignVillageTownEntity::getId));

        List<SignVillageTownEntity> records = page.getRecords();
        for (SignVillageTownEntity record : records) {
            // 查询 SignContainerEntity 表，当 village_id 属性与当前 record 的 id 相同时
            List<SignContainerEntity> containers = signContainerService.list(new LambdaQueryWrapper<SignContainerEntity>().eq(SignContainerEntity::getVillageId, record.getId()));
            record.setContainers(containers);
        }
        page.setRecords(records);
        return new PageUtils(page);
    }

    @Override
    public Map<String, SignVillageTownEntity> getSensorVillageTownMap() {
        return null;
    }


}